package com.youu.youu.manager.shiro;

import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.youu.youu.manager.bean.TPermiss;
import com.youu.youu.manager.bean.TRole;
import com.youu.youu.manager.dao.TPermissMapper;
import com.youu.youu.manager.service.RolesBackstageService;

public class FilterChainDefinitionMapBuilder {
	@Autowired
	RolesBackstageService rBackstageService;
	@Autowired
	TPermissMapper tPermissMapper;

	public LinkedHashMap<String, String> buildFilterChainDefinitionMap() {
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.putAll(controlPermissByRole());
		map.put("/**/*.css", "anon");
		map.put("/**/*.js", "anon");
		map.put("/**/*.png", "anon");
		map.put("/**/*.jpg", "anon");
		map.put("/**/*.eot", "anon");
		map.put("/**/*.woff", "anon");
		map.put("/**/*.ttf", "anon");
		map.put("/**/*.svg", "anon");
		map.put("/logout.html", "logout");
		map.put("/manager/login", "anon");
		map.put("/index.html", "roles[manager]");
		map.put("/shiro/login", "anon");
		map.put("/shiro/logout", "logout");
		map.put("/user.jsp", "authc,roles[user]");
		map.put("/admin.jsp", "authc,roles[admin]");
		map.put("/list.jsp", "user");
		map.put("/**", "authc");
		return map;
	}

	public LinkedHashMap<String, String> controlPermissByRole() {
		List<TRole> roles = rBackstageService.getAllRoles();
		LinkedHashMap<String, String> map = new LinkedHashMap<>();
		for (TRole tRole : roles) {
			String roleName = tRole.getRoleName();
			List<TPermiss> tPermisses = tPermissMapper
					.selectPermissByOneRole(tRole);
			if (tPermisses != null) {
				for (TPermiss tPermiss : tPermisses) {
					String url = tPermiss.getUrl();
					if (!StringUtils.isEmpty(url)) {
						map.put(url, "roles[" + roleName + "]");
					}
				}
			}
		}
		return map;

	}

}
